package cn.lysj.framework.common.validate.validate;

import cn.lysj.framework.common.validate.annotation.Range;
import cn.lysj.framework.common.validate.entity.Entity;

import java.math.BigDecimal;

public class RangeValidate extends AbstractValidate<Range> {

    public boolean validate(Entity entity) {
        if (null == entity.getValue()) return false;
        BigDecimal arg = null;
        BigDecimal max = null;
        BigDecimal min = null;
        try {
            arg = new BigDecimal(String.valueOf(entity.getValue()));
            max = new BigDecimal(annotation.max());
            min = new BigDecimal(annotation.min());
        }catch (Exception e){
            return false;
        }
        return arg.compareTo(min) != -1 && arg.compareTo(max) != 1;
    }

    public void init() {
        this.setMsg(annotation.message());
        this.setGroups(annotation.groups());
    }
}
